All Articles

Template for simple (bad-practice) python yaml config loader with args

Warning: this is probably not considered best practice in your place of work. I use it because it works for me. YMMV

For many of my python projects, I often find myself adding a simple config class up top with some class variables I use for quickly testing/changing the way the file runs. However, quite often when I want to run some ‘pseudo-production’ tests, I need to run various different configs via some bash script/the cli (e.g. for different ML models on the HPC). My problem: I don’t really want to change the files, as I still want to access my config variables with ...if config.myvar ..., but setting up a custom argparser every time can be cumbersome. Solution: use this (bad-practice) template:

Normally, my config would be along the lines of:

class config:
    myvar = "hello world"

if config.myvar == "hello world": ...

Now I simply add the following up top (the magic template):

import argparse, yaml

parser = argparse.ArgumentParser(description='Configure a jconfig')
parser.add_argument('-c', '--config', help='Config .yaml file path', type=str, default='./config.yaml')
args = parser.parse_args()

# Load config file
with open(args.config, 'r') as f:
    configyaml = yaml.load(f, Loader=yaml.FullLoader)

class config:
    for key, value in configyaml.items():
        locals()[key] = value

add a yaml file:

myvar : "hello world"

and it’s ready to go.